package com.wjq.pay.service;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.wjq.pay.config.AlipayConfig;

import java.util.HashMap;
import java.util.Map;

public class AlipayService {


    public String getQrCode(String orderNo) throws AlipayApiException {

        CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
        certAlipayRequest.setServerUrl(AlipayConfig.url);
        certAlipayRequest.setAppId(AlipayConfig.APP_ID);
        certAlipayRequest.setPrivateKey(AlipayConfig.APP_PRIVATE_KEY);
        certAlipayRequest.setFormat("json");
        certAlipayRequest.setCharset("utf-8");
        certAlipayRequest.setSignType("RSA2");
        certAlipayRequest.setCertPath(AlipayConfig.APP_CERT_PATH);

        certAlipayRequest.setAlipayPublicCertPath(AlipayConfig.ALIPAY_CERT_PATH);
        certAlipayRequest.setRootCertPath(AlipayConfig.ALIPAY_ROOTCERT_PATH);
        DefaultAlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
//        AlipayClient alipayClient = new DefaultAlipayClient(payConfig.getUrl(), payConfig.getAppid(), payConfig.getAppPrivateKey(), "json", "utf-8", payConfig.getAlipayPublicKey(), "RSA2");

        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();//创建API对应的request类
        Map<String,String> maps = new HashMap<String,String>();
        maps.put("out_trade_no",orderNo);
        maps.put("total_amount","0.01");
        maps.put("subject","Iphone6 16G");
        maps.put("store_id","NJ_001");
        maps.put("timeout_express","3600m");
        request.setBizContent(JSON.toJSONString(maps));
        //设置回调的url地址 关键
        request.setNotifyUrl(AlipayConfig.CALL_BACK_URL);
        AlipayTradePrecreateResponse response = alipayClient.certificateExecute(request);
        System.out.print(response.getBody());
        String qrcode = response.getQrCode();
        return qrcode;
    }
}